package com.ghj.packageoftest;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.ghj.packageofdomain.Data;
import com.ghj.packageofdomain.Device;
import com.ghj.packageofdomain.DeviceInfo;
import com.ghj.packageofdomain.Devices;
import com.ghj.packageofdomain.HttpBean;
import com.ghj.packageofdomain.Meta;
import com.ghj.packageofdomain.Response;
import com.ghj.packageofdomain.User;

public class Test {

	public static void main(String[] args) {
		// System.out.println(objectToJsonString());
		// jsonStringToObject(objectToJsonString());

		System.out.println(collectionToJsonString());
//		jsonStringToCollection(collectionToJsonString());
	}

	/**
	 * 将对象转换为Json字符串
	 * 
	 * @author 高焕杰
	 */
	static String objectToJsonString() {
		JSONObject jsonObject = JSONObject.fromObject(new User(1, "张三", "男"));
		return jsonObject.toString();
	}

	/**
	 * 将Json字符串转换为对象
	 * 
	 * @author 高焕杰
	 */
	static void jsonStringToObject(String jsonString) {
		JSONObject jsonObject = JSONObject.fromObject(jsonString);
		User user = (User) JSONObject.toBean(jsonObject, User.class);
		System.out.println(user);
	}

	/**
	 * 将集合转换为Json字符串
	 * 
	 * @author 高焕杰
	 */
	static String collectionToJsonString() {
		// List<User> userList = new ArrayList<User>();
		// userList.add(new User(1, "张三", "男"));
		// userList.add(new User(2, "李四", "女"));
		// userList.add(new User(3, "王五", "男"));
		// userList.add(new User(4,"mazi"));
		// JSONArray jsonArray = JSONArray.fromObject(userList);
		// return jsonArray.toString();


		JSONObject jsonStr = JSONObject.fromObject(new HttpBean(new Meta("1.1.0"), new Response(200, "登录成功", "command"),
				new Devices.Datas("devices")));
		return jsonStr.toString();
		
//		List<Device> deviceList = new ArrayList<Device>();
//		DeviceInfo info = new DeviceInfo("deviceId", "serialNumber", "version",
//				"batteryPower", "wifiSSID", "deviceName", 1);
//		DeviceInfo info = new Device(new DeviceInfo("deviceId11", "serialNumber11", "version11",
//				"batteryPower11", "wifiSSID11", "deviceName11", 1);
//		deviceList.add(new Device(new DeviceInfo("deviceId11", "serialNumber11", "version11",
//				"batteryPower11", "wifiSSID11", "deviceName11", 1), 2, 3, "manager11", 4, true));
//		deviceList.add(new Device(new DeviceInfo("deviceId22", "serialNumber22", "version22",
//				"batteryPower22", "wifiSSID22", "deviceName22", 1), 2, 3, "manager", 4, true));
//		deviceList.add(new Device(new DeviceInfo("deviceId33", "serialNumber33", "version33",
//				"batteryPower33", "wifiSSID33", "deviceName33", 1), 2, 3, "manager33", 4, true));
//		JSONArray jsonArray = JSONArray.fromObject(deviceList);
//		return jsonArray.toString();
		
		
		
	}

	/**
	 * 将Json字符串转换为集合
	 * 
	 * @author 高焕杰
	 */
	static void jsonStringToCollection(String jsonString) {
		JSONArray jsonArray = JSONArray.fromObject(jsonString);
		@SuppressWarnings("unchecked")
		// List<User> userList = (List<User>)JSONArray.toCollection(jsonArray,
		// User.class);
		// for (User user : userList) {
		// System.err.println(user);
		// }
		List<Device> userList = (List<Device>) JSONArray.toCollection(
				jsonArray, Device.class);
		for (Device device : userList) {
			System.out.println(device.toString());
		}
	}

}